package com.example.translateappjava.Activity.HomeFragmentActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.translateappjava.R;
import com.example.translateappjava.Service.ChatGPT.ChatGptHelper;
import com.example.translateappjava.Service.MedicalAtlas.MedicalAtlasHelper;
import com.example.translateappjava.Utils.Message;
import com.example.translateappjava.Utils.MessageAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RobotActivity extends AppCompatActivity implements ChatGptHelper.OnChatGptResponseListener{
    private List<Message> messageList;
    private MessageAdapter messageAdapter;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_robot);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        EditText editTextMessage = findViewById(R.id.editTextMessage);
        Button buttonSend = findViewById(R.id.buttonSend);

        messageList = new ArrayList<>();
        messageAdapter = new MessageAdapter(messageList);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(messageAdapter);

        buttonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String messageText = editTextMessage.getText().toString().trim();
                if (!messageText.isEmpty()) {
                    sendMessage(messageText);
                    editTextMessage.setText("");
                }
            }
        });
    }

    private void sendMessage(String messageText) {
        // Add the user's message to the list and notify adapter
        messageList.add(new Message(messageText, true));
        messageAdapter.notifyItemInserted(messageList.size() - 1);

        // Simulate a chatbot response
        simulateBotResponse(messageText);
    }

    private void simulateBotResponse(String messageText) {
        ChatGptHelper.OnChatGptResponseListener chatGptResponseListener = (ChatGptHelper.OnChatGptResponseListener) RobotActivity.this;
        ChatGptHelper chatGptHelper = new ChatGptHelper(RobotActivity.this);
        chatGptHelper.setOnChatGptResponseListener(chatGptResponseListener);
        chatGptHelper.chatGPTRequest(messageText);

        String botResponse = "Robot said: " + messageText;

        // Add the bot's response to the list and notify adapter

    }


    @Override
    public void onChatGptResponse(String response) {
        try {
            JSONObject jsonResponse = new JSONObject(response.toString());
            JSONArray choices = jsonResponse.getJSONArray("choices");
            JSONObject message = choices.getJSONObject(0).getJSONObject("message");
            String content = message.getString("content");
            messageList.add(new Message(content, false));
            messageAdapter.notifyItemInserted(messageList.size() - 1);
        }
        catch (JSONException e){
            e.printStackTrace();
        }

    }

    @Override
    public void onError(String message) {

    }
}
